package com.szmg.auth.web.controller;


import java.util.List;
import javax.annotation.Resource;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
import com.szmg.auth.model.User;
import com.szmg.auth.service.UserService;



@Controller
public class UserController {
	
	public static final int pagesize = 8;
	
	@Resource(name="userService")
	private UserService userService;
	
	@RequestMapping(value="/manager/auth/users",method=RequestMethod.GET)
	public ModelAndView listUser(String pagenum,User user){
		ModelAndView mv=new ModelAndView();
		mv.setViewName("auth/users");
		mv.addObject("sidebar","users");
		int num = 1;
		if(null!=pagenum){
			num = Integer.parseInt(pagenum);
		}
		List<User> list = userService.listUser((num-1)*pagesize, pagesize,user);
		mv.addObject("userList", list);
		mv.addObject("length", list.size());
		mv.addObject("pagenum", num);
		mv.addObject("user", user);
		return mv;
	}

	
	
	@RequestMapping(value="/manager/auth/adduser",method=RequestMethod.POST)
	public ModelAndView adduser(User user){
		ModelAndView mv=new ModelAndView();
		mv.setViewName("redirect:/manager/users");
		userService.addUser(user);
		mv.addObject("notice","添加成功");
		return mv;
	}
	
	@RequestMapping(value="/manager/auth/deleteuser",method=RequestMethod.GET)
	public ModelAndView deleteuser(int userid){
		ModelAndView mv=new ModelAndView();
		mv.setViewName("redirect:/manager/auth/users");
		userService.deleteUserById(userid);
		mv.addObject("notice","删除成功");
		return mv;
	}
	
	
}
